package chemins.pack;

import accueil.pack.Menu;
import accueil.pack.SousMenu;
import chemin.pack.R;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.*;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.opengl.Visibility;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.Layout;
import android.util.Log;
import android.view.*;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.BounceInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.*;
import communication.AndroidConnection;
import communication.IncomingHandler;
import java.text.DecimalFormat;

public class CheminActivity extends Activity {

	DrawViewChemin cheminView;
        
        MyMediaPlayer mediaPlayer;
        ViewGroup blocVideosSamplesLayout;
        LayoutParams videoParams,sampleContainerParams,blocBlancParams,blocNoirParams;
        
                
	AndroidConnection conn;
        IncomingHandler handler;
	EditText distanceTotaleText;
        String typeDeChemin;
        
        Double energieTotale;
        Double energieProduite;
        Double energieDistribuee;
        Double energieProduiteSpeedSensor;
        Double energieProduiteTorqueSensor;
        
        Utilisateur utilisateur = new Utilisateur();
        
        Boolean infoUtilisateurtamponne = false;
        String assistanceType;
        
        SharedPreferences preferences;
        String texteCadreJuridique;
        
        int distanceParcourue = 0;     
        int credit = 500;
        int creditInitial = 500;
        int pathNumber;
        LinearLayout.LayoutParams jaugeCreditD= new LinearLayout.LayoutParams(0,LayoutParams.MATCH_PARENT,0);
        LinearLayout.LayoutParams jaugeCreditG= new LinearLayout.LayoutParams(0,LayoutParams.MATCH_PARENT,0);
        
        DecimalFormat df = new DecimalFormat ( ) ;
        private View view;
        

        ViewGroup layout;
    
        Typeface policeAltermove;
     
        
        
        
    @Override
    protected void onStart() {
        conn = new AndroidConnection(handler);
        
        if(typeDeChemin.equals("monChemin"))
        {
            conn.initialize();
        }
        super.onStart();
    }

    @Override
    protected void onStop() {
        
        conn.close();
        super.onStop();
    }

     @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitActivity(view);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        
        
        
                
                    super.onCreate(savedInstanceState);
  
                    
                    
                    preferences = getSharedPreferences("myPreferences",MODE_MULTI_PROCESS);
                    texteCadreJuridique = preferences.getString("texteCadreJuridique", "defaut");
                    this.texteCadreJuridique = texteCadreJuridique;
                    
                    
                    
                    df.setMinimumFractionDigits( 0 ) ;
                    df.setMaximumFractionDigits( 1 );
                    df.setDecimalSeparatorAlwaysShown( false ) ; 
                            
                    handler = new IncomingHandler(){


                        public void handleMoveChange(String messageBT) {

                            
                            if(cheminView.velo.distanceParcourue >= cheminView.distanceFinale)
                            {
                                cheminView.velo.distanceParcourue = cheminView.distanceFinale;
                                                                
                                                              
                                if(cheminView.parcours.boucle == true)
                                {
                                    initialisationDistanceParcourue(view);
                                }
                                else
                                {
                                    exitActivity(view);
                                }
                            }
                            else
                            {
                               
                                
                                if(cheminView.velo.distanceParcourue != Integer.parseInt(messageBT.substring(0,messageBT.indexOf(",", 0)))) 
                                {
                                    if ((((creditInitial != -1)))&&(!cheminView.typeChemin.equals("cheminTDF")))
                                    {
                                        credit--;                                    
                                        evolutionCredit();
                                    }
                                    utilisateur.setDistanceSession(utilisateur.getDistanceSession() + 1);
                                    
                                }
                                
                                //________récupération de la distance parcourue envoyées par bluetooth

                                cheminView.velo.distanceParcourue = Integer.parseInt(messageBT.substring(0,messageBT.indexOf(",", 0)));

                                


                                //________récupération des énergies envoyées par bluetooth
                                messageBT = messageBT.substring(messageBT.indexOf(",")+1);
                                energieTotale = Double.parseDouble(messageBT.substring(0,messageBT.indexOf(",")));

                                
                                messageBT = messageBT.substring(messageBT.indexOf(",")+1);

                                energieProduiteSpeedSensor = Double.parseDouble(messageBT.substring(0,messageBT.indexOf(",")));

                                energieProduiteTorqueSensor = Double.parseDouble(messageBT.substring(messageBT.lastIndexOf(",")+1));


                    
                                traitementEnergies();
          
                                
                            }

                            cheminView.invalidate();

                        }
                        



                        @Override
                        public void handleSendMsg(String string) {

                                conn.write(string);                              

                                


                        }

                        @Override
                        public void handleReset() {

                            conn.write("reset");
                        }

                        @Override
                        public void handleReconnect() {

                            if(!conn.write("slope:"+cheminView.getSlope())){

                                return;
                            }

                            conn.write("distance:"+cheminView.getDistance());
                        }

                        @Override
                        public void handleData(String substring) {

                            if(substring.startsWith("maxTime")){
                                handler.sendMessageDelayed(handler.obtainMessage(IncomingHandler.EXIT), Integer.parseInt(substring.substring(7)));
                            }
                        }




                    };


                    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
                    requestWindowFeature(Window.FEATURE_NO_TITLE);


                    Bundle extras=getIntent().getExtras();
                    
                    //_________________________ initialisation crédit
                    credit =extras.getInt("credit");
                    this.credit = credit;
                    creditInitial =extras.getInt("creditInitial");
                    this.creditInitial = creditInitial;

                    //_________________________ initialisation type d assistance
                    assistanceType =extras.getString("assistanceType");
                    this.assistanceType = assistanceType;
                    
                    //_________________________initialisation utilisateur
                    
                    utilisateur.setDistanceTotale(extras.getInt("distanceParcourueTotale"));
                    this.utilisateur.setDistanceSession(utilisateur.getDistanceSession());
                    
                    
                    
                    typeDeChemin =extras.getString("typeDeChemin");
                    
                    
                            
                    if(typeDeChemin.equals("monChemin"))
                    {
                        setContentView(R.layout.mon_chemin_interface);
                        
                        
                        
                        
                        
                        if(creditInitial == -1)
                        {
                            findViewById(R.id.monCheminJaugeBase).setVisibility(View.GONE);                            
                        }
                        
                        cheminView = new DrawViewChemin(this, null,handler,true,"monChemin");



                        distanceTotaleText = (EditText) findViewById(R.id.monCheminDistanceChoixTextView);
                        distanceTotaleText.setText("1000");


                        layout = (ViewGroup) findViewById(R.id.monChemin_layout);
                        
                        initialisationTextMonChemin();
                        evolutionCredit();
                        
                        cheminView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                        
                        layout.addView(cheminView);
                    }
                    if(typeDeChemin.equals("cheminTDF"))
                    {
                        int i = 0;
                        setContentView(R.layout.chemin_tdf_interface);
                        

                        
                        if(creditInitial == -1)
                        {
                            if(typeDeChemin.equals("cheminTDF"))
                            {
                                findViewById(R.id.cheminTDFJaugeBase).setVisibility(View.GONE);
                            }
                            if(typeDeChemin.equals("monChemin"))
                            {
                                findViewById(R.id.monCheminJaugeBase).setVisibility(View.GONE);
                            }
                                                      
                        }
                        
                        findViewById(R.id.cheminTDFRestartButton).setVisibility(View.GONE);
                        findViewById(R.id.cheminTDFParticipationALEffortTotalValueTextView).setVisibility(View.GONE);
                        findViewById(R.id.cheminTDFEnergieDepenseeValueTextView).setVisibility(View.GONE);
                        findViewById(R.id.cheminTDFZoomButtonLayout).setVisibility(View.GONE);

                        cheminView = new DrawViewChemin(this, null,handler,false,"cheminTDF");
                        cheminView.parcours.setSize(extras.getInt("size"));

                        while(i < cheminView.parcours.getSize())
                        {

                        Double distance = extras.getDouble("indice"+i+"distance");
                        Float hauteur = extras.getFloat("indice"+i+"hauteur");


                        cheminView.parcours.tab[i]= new Point();
                        cheminView.parcours.tab[i].setDistance(distance);
                        cheminView.parcours.tab[i].setHauteur(hauteur);
                        i++;
                        }
                        cheminView.parcours.setSize(i);                        
                        cheminView.miseEnFormeCheminTDF();


                        

                        ViewGroup layout = (ViewGroup) findViewById(R.id.cheminTDF_layout);
                        
                        initialisationTextCheminTDF();
                        evolutionCredit();
                        
                        
                        cheminView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                        layout.addView(cheminView);

                    }

        
                    //_________________________ initialisation vidéos
                    
                    
                    
                    miseEnFormeMediaPlayerView();
                    closeTuto(view);
                    
                    
                    

    }

/*_______________________________________________Fonctions gestion du chemin______________________________________________________________*/

  
    public void initialisationChemin(View bouton) {
    	

        handler.sendMessage(handler.obtainMessage(IncomingHandler.SEND_MESSAGE,"distance:0"));
    	initialisationDistanceParcourue(bouton);

        cheminView.initialiserChemin();
    	cheminView.invalidate();
    	
    }
    
    public void initialisationDistanceParcourue(View bouton) {

        handler.sendMessage(handler.obtainMessage(IncomingHandler.SEND_MESSAGE,"distance:0"));
        cheminView.velo.distanceParcourue = 0;
        cheminView.velo.pente = 0;
    
        cheminView.invalidate();

    }
    
      public void validerParcelle(View v)
  {
      
        conn.initialize();
        
        if(cheminView.modifierParcoursSelonPortions() != false)
        {

            

            findViewById(R.id.cheminTDFValidationButton).setVisibility(View.GONE);
            findViewById(R.id.cheminTDFValidationTextView).setVisibility(View.GONE);
            findViewById(R.id.cheminTDFRestartButton).setVisibility(View.VISIBLE);
            findViewById(R.id.cheminTDFZoomButtonLayout).setVisibility(View.VISIBLE);
            findViewById(R.id.cheminTDFParticipationALEffortTotalValueTextView).setVisibility(View.VISIBLE);
            findViewById(R.id.cheminTDFEnergieDepenseeValueTextView).setVisibility(View.VISIBLE);

            initialisationDistanceParcourue(v);


            cheminView.invalidate();
        }
        
  }
    
  public void validerDistanceTotale(View bouton) {
    	
        int i;
        boolean stringIsNumeric = true;
        CharSequence c = distanceTotaleText.getText().toString();

        for(i=0;i<c.length();i++)
        {
            if (Character.isDigit(c.charAt(i)) == false)
            {
                stringIsNumeric = false;
            }
        }
        
        if(stringIsNumeric == true)
        {
            cheminView.distanceTotale= Integer.parseInt(distanceTotaleText.getText().toString());
            initialisationChemin(bouton);
        }


    }
  
  public void echelleZoomer(View bouton) {
  	
	  int i = 0;
	  
	  for(i=0;i<=cheminView.parcours.getSize()-1;i++)
	  {
		  
	  
		  if((-(cheminView.echelle.ponderation - 1)*(cheminView.y_max) + (cheminView.echelle.ponderation*cheminView.parcours.tab[i].y)) <= cheminView.y_min)
		  {
			  return;
		  }
		  
	  }
	  
	  i=0;
	  cheminView.echelle.value = cheminView.echelle.value / cheminView.echelle.ponderation;
	  cheminView.invalidate();

	  while(i<=cheminView.parcours.getSize()-1)
  	{
		  cheminView.parcours.tab[i].y= -(cheminView.echelle.ponderation - 1)*(cheminView.y_max) + (cheminView.echelle.ponderation*cheminView.parcours.tab[i].y);
		  i++;
  	}
	  
	  
	 
  }
  
  public void echelleDezoomer(View bouton) {
	 
	  int i = 0;
	  

	  cheminView.echelle.value = cheminView.echelle.value * cheminView.echelle.ponderation;
	  cheminView.invalidate();
	  

	  
	  while(i<=cheminView.parcours.getSize()-1)
  	{
		  cheminView.parcours.tab[i].y= (cheminView.parcours.tab[i].y + (cheminView.y_max)*(cheminView.echelle.ponderation - 1))/cheminView.echelle.ponderation;
		  i++;
  	}
  }
  
  public void exitActivity(View v)
  {
        utilisateur.setDistanceTotale(utilisateur.getDistanceTotale()+utilisateur.getDistanceSession());
        
        handler.sendMessage(handler.obtainMessage(IncomingHandler.SEND_MESSAGE,"slope:"+Integer.toString(0)));
        handler.sendMessage(handler.obtainMessage(IncomingHandler.SEND_MESSAGE,"distance:0"));
        versTableauRecapitulatif();

  }
  
  
  /*__________________________________________________Gestion du Media Player______________________________________________*/
  
  public void lancerMediaPlayer(View v)
  {
      
      if((typeDeChemin.equals("cheminTDF"))&&(findViewById(R.id.cheminTDFMediaPlayer_fond).getVisibility()==View.GONE))
      {
          findViewById(R.id.cheminTDFMediaPlayer_fond).setVisibility(View.VISIBLE);

      }
      
      if((typeDeChemin.equals("monChemin"))&&(findViewById(R.id.monCheminMediaPlayer_fond).getVisibility()==View.GONE))
      {
          findViewById(R.id.monCheminMediaPlayer_fond).setVisibility(View.VISIBLE);
          
      }
      
  	cheminView.touchable = false;         

  }
   public void closeTuto(View v)
  {
        
        pauseVideo(v);
        if(typeDeChemin.equals("cheminTDF"))
        {
            findViewById(R.id.cheminTDFMediaPlayer_fond).setVisibility(View.GONE);
        }
        if(typeDeChemin.equals("monChemin"))
        {
            findViewById(R.id.monCheminMediaPlayer_fond).setVisibility(View.GONE);
        }

        cheminView.touchable = true;
                   

  }
   
   
  public void playVideo(View v)
  {
      if(!mediaPlayer.mainVideo.isPlaying())
  	{
            mediaPlayer.mainVideo.start();
        }

  }
  
  public void restartVideo(View v)
  {
  	
  	mediaPlayer.mainVideo.seekTo(10);             

  }
  
  public void pauseVideo(View v)
  {
  	if(mediaPlayer.mainVideo.isPlaying())
  	{
  		mediaPlayer.mainVideo.pause();                
  	}
                 

  }
  
  public void nextVideo(View v)
  {
      if(mediaPlayer.listeFichierMP4.length > 0)
      {
          if(mediaPlayer.listeSampleContainers[mediaPlayer.listeSampleContainers.length-1].getX()>= mediaPlayer.videoSampleXInit)
        {
            
            int i;
            float distanceDeTransfert = -255;
            
            if(mediaPlayer.listeSampleContainers[mediaPlayer.listeSampleContainers.length-1].getX() + distanceDeTransfert < mediaPlayer.videoSampleXInit)
            {
                distanceDeTransfert = mediaPlayer.videoSampleXInit -  mediaPlayer.listeSampleContainers[mediaPlayer.listeSampleContainers.length-1].getX();
            }

            for(i=0;i<mediaPlayer.listeSampleContainers.length;i++)
            {
                mediaPlayer.listeSampleContainers[i].animate().xBy(distanceDeTransfert).setDuration(500);
                //mediaPlayer.listeTitres[i].animate().xBy(distanceDeTransfert).setDuration(500);
            }
            for(i=0;i<mediaPlayer.listeBlocs.length;i++)
            {
                mediaPlayer.listeBlocs[i].animate().xBy(distanceDeTransfert).setDuration(500);
            }
        }
      }
      
   
  }
  public void videoBefore(View v)
  {
      if(mediaPlayer.listeFichierMP4.length > 0)
      {
          if(mediaPlayer.listeSampleContainers[0].getX() <= mediaPlayer.videoSampleXInit)
        {
            int i;
            float distanceDeTransfert = 255;
            
            if(mediaPlayer.listeSampleContainers[0].getX() + distanceDeTransfert > mediaPlayer.videoSampleXInit)
            {
                distanceDeTransfert = mediaPlayer.videoSampleXInit - mediaPlayer.listeSampleContainers[0].getX();
            }
            
            for(i=0;i<mediaPlayer.listeSampleContainers.length;i++)
            {            
                mediaPlayer.listeSampleContainers[i].animate().xBy(distanceDeTransfert).setDuration(500);
                
                //mediaPlayer.listeTitres[i].animate().xBy(distanceDeTransfert).setDuration(500);
            }
            for(i=0;i<mediaPlayer.listeBlocs.length;i++)
            {
                mediaPlayer.listeBlocs[i].animate().xBy(distanceDeTransfert).setDuration(500);
            }
        }
      }
        
 }

    
  public void gestionAffichageButtonMediaPlayer(View v)
  {
      findViewById(R.id.myMediaPlayerNextVideoButtonImageView).setVisibility(View.VISIBLE);
      findViewById(R.id.myMediaPlayerVideoBeforeButtonImageView).setVisibility(View.VISIBLE);
      
  }
         
  
 public void miseEnFormeMediaPlayerView()
  {
    int i;
    blocVideosSamplesLayout = (ViewGroup) findViewById(R.id.myMediaPlayerBlocVideosSamples);         
    sampleContainerParams = new LayoutParams(230, LayoutParams.FILL_PARENT);    
    videoParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,Gravity.CENTER);
    blocBlancParams = new LayoutParams(10, 120);
    blocNoirParams = new LayoutParams(5, 120);
    blocNoirParams.gravity = Gravity.CENTER;
    
    ViewGroup layout = (ViewGroup) findViewById(R.id.myMediaPlayerBlocVideosSamples);
    
    policeAltermove = Typeface.createFromAsset(getAssets(), "eurosti.ttf");
    
    mediaPlayer = new MyMediaPlayer();    
                    

    
    if(mediaPlayer.listeFichierMP4.length > 0)
    {
        mediaPlayer.mainVideo = (VideoView)findViewById(R.id.myMediaPlayerVideoView);
        mediaPlayer.mainVideo.setOnPreparedListener(new OnPreparedListener(){


                public void onPrepared(MediaPlayer arg0) {
                mediaPlayer.mainVideo.seekTo(10);
                }
      
        });
        
        mediaPlayer.mainVideo.setVideoPath(mediaPlayer.listeFichierMP4[0].getAbsolutePath());
        
        for(i=0;i<mediaPlayer.listeFichierMP4.length;i++)
        {
           
            mediaPlayer.listeSampleContainers[i] = new LinearLayout(this);
            mediaPlayer.listeSampleContainers[i].setLayoutParams(sampleContainerParams);
            mediaPlayer.listeSampleContainers[i].setOrientation(LinearLayout.VERTICAL);
            layout.addView(mediaPlayer.listeSampleContainers[i]);

            FrameLayout imageContainer = new FrameLayout(getApplicationContext());
            imageContainer.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,90));        
            mediaPlayer.listeSampleContainers[i].addView(imageContainer);
            pathNumber = i;
            mediaPlayer.listeSampleContainers[i].setOnClickListener(new OnClickListener() {

                int i = pathNumber;
                    public void onClick(View view) {
                        String videoPath;
                        videoPath = mediaPlayer.listeFichierMP4[i].getAbsolutePath();
                        lancerVideo(videoPath);
                    }



            });

            mediaPlayer.listeImages[i] = new ImageView(getApplicationContext());
            mediaPlayer.listeImages[i].setLayoutParams(videoParams);
            imageContainer.addView(mediaPlayer.listeImages[i]);
            Bitmap myBitmap;
            String myBitmapPath = mediaPlayer.listeFichierMP4[i].getAbsolutePath().substring(0,mediaPlayer.listeFichierMP4[i].getAbsolutePath().length() -3)+"PNG";
            
            if(BitmapFactory.decodeFile(myBitmapPath) != null)
            {
                myBitmap = BitmapFactory.decodeFile(myBitmapPath);

            }
            else
            {
                 myBitmap = BitmapFactory.decodeFile("/mnt/storage/App Chemin/MediaPlayer/Config/videoSampleDefaut.PNG");
            }
            mediaPlayer.listeImages[i].setImageBitmap(myBitmap);


            mediaPlayer.listeTitres[i] = new TextView(getApplicationContext());
            mediaPlayer.listeSampleContainers[i].addView(mediaPlayer.listeTitres[i]);
            CharSequence titre = mediaPlayer.listeFichierMP4[i].getAbsolutePath().substring(mediaPlayer.listeFichierMP4[i].getAbsolutePath().lastIndexOf("/")+1);
            titre = titre.subSequence(0, titre.length() -3);
            mediaPlayer.listeTitres[i].setText(titre);
            mediaPlayer.listeTitres[i].setTextColor(Color.BLACK);
            mediaPlayer.listeTitres[i].setGravity(Gravity.CENTER);
            mediaPlayer.listeTitres[i].setTypeface(policeAltermove);
            mediaPlayer.listeTitres[i].setTextSize(18);

            mediaPlayer.listeBlocs[i*3 ] = new LinearLayout(getApplicationContext());
            mediaPlayer.listeBlocs[i*3 ].setLayoutParams(blocBlancParams);
            mediaPlayer.listeBlocs[i*3 ].setBackgroundColor(Color.WHITE);
            layout.addView(mediaPlayer.listeBlocs[i*3 ]);


            mediaPlayer.listeBlocs[i*3 +1] = new LinearLayout(getApplicationContext());
            mediaPlayer.listeBlocs[i*3 +1].setLayoutParams(blocNoirParams);
            mediaPlayer.listeBlocs[i*3 +1].setBackgroundColor(Color.BLACK);
            layout.addView(mediaPlayer.listeBlocs[i*3 +1]);

            mediaPlayer.listeBlocs[i*3 +2] = new LinearLayout(getApplicationContext());
            mediaPlayer.listeBlocs[i*3 +2].setLayoutParams(blocBlancParams);
            mediaPlayer.listeBlocs[i*3 +2].setBackgroundColor(Color.WHITE);
            layout.addView(mediaPlayer.listeBlocs[i*3 +2]);






        }

        mediaPlayer.videoSampleXInit = mediaPlayer.listeSampleContainers[0].getX();
    }
    



  }
  
    public void lancerVideo(String videoPath)
    {

            
            videoPath = videoPath.substring(0, videoPath.length()-4) + ".mp4";

            mediaPlayer.mainVideo.stopPlayback();
            mediaPlayer.mainVideo.setVideoPath(videoPath);      

        

    }
  
  

  
  
  

  
  /*_________________________________________________________________________________________________________________________________*/
  
  public void bouclerChemin(View v)
  {
      if(cheminView.parcours.boucle == false)
      {
          cheminView.parcours.boucle = true;
          ((TextView) findViewById(R.id.monCheminBoucleTextView)).setText("boucle : ON");
      }
      else
      {
          cheminView.parcours.boucle = false;
          ((TextView) findViewById(R.id.monCheminBoucleTextView)).setText("boucle : OFF");
      }
      
  }
  
  public void initialisationTextCheminTDF()
            {

                policeAltermove = Typeface.createFromAsset(getAssets(), "eurosti.ttf");
                ((TextView) findViewById(R.id.cheminTDFParticipationALEffortTotalTextView)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.cheminTDFParticipationALEffortTotalTextView)).setTextSize(24);
                
                ((TextView) findViewById(R.id.cheminTDFEnergieDepenseeTextView)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.cheminTDFEnergieDepenseeTextView)).setTextSize(24);
                
                ((TextView) findViewById(R.id.cheminTDFValidationTextView)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.cheminTDFValidationTextView)).setTextSize(24);
                
                ((TextView) findViewById(R.id.cheminTDFParticipationALEffortTotalValueTextView)).setTypeface(policeAltermove);                
                ((TextView) findViewById(R.id.cheminTDFParticipationALEffortTotalUniteeTextView)).setTypeface(policeAltermove);
                
                ((TextView) findViewById(R.id.cheminTDFEnergieDepenseeValueTextView)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.cheminTDFEnergieDepenseeUniteeTextView)).setTypeface(policeAltermove);
                
                ((TextView) findViewById(R.id.cheminTDFRestartTextView)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.cheminTDFRestartTextView)).setTextSize(20);
                ((TextView) findViewById(R.id.cheminTDFEchelleTextView)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.cheminTDFEchelleTextView)).setTextSize(20);
                
                ((TextView) findViewById(R.id.cheminTDFFenetreJuridiqueTextView)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.cheminTDFFenetreJuridiqueTextView)).setTextSize(15);
                ((TextView) findViewById(R.id.cheminTDFFenetreJuridiqueTextView)).setText(texteCadreJuridique);

            }
  
   public void initialisationTextMonChemin()
            {
                
                policeAltermove = Typeface.createFromAsset(getAssets(), "eurosti.ttf");
                ((TextView) findViewById(R.id.monCheminParticipationALEffortTotalTextView)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.monCheminParticipationALEffortTotalTextView)).setTextSize(24);
                
                ((TextView) findViewById(R.id.monCheminEnergieDepenseeTextView)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.monCheminEnergieDepenseeTextView)).setTextSize(24);
                
                ((TextView) findViewById(R.id.monCheminParticipationALEffortTotalValueTextView)).setTypeface(policeAltermove);                
                ((TextView) findViewById(R.id.monCheminParticipationALEffortTotalUniteeTextView)).setTypeface(policeAltermove);
                
                ((TextView) findViewById(R.id.monCheminEnergieDepenseeValueTextView)).setTypeface(policeAltermove);                
                ((TextView) findViewById(R.id.monCheminEnergieDepenseeUniteeTextView)).setTypeface(policeAltermove);
                
                ((TextView) findViewById(R.id.monCheminDistanceChoixTextView)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.monCheminDistanceTextView)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.monCheminDistanceTextView)).setTextSize(24);
                
                ((TextView) findViewById(R.id.monCheminDistanceUniteTextView)).setTypeface(policeAltermove);
                
                
                ((TextView) findViewById(R.id.monCheminRestartTextView)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.monCheminRestartTextView)).setTextSize(20);
                ((TextView) findViewById(R.id.monCheminEchelleTextView)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.monCheminEchelleTextView)).setTextSize(20);
                ((TextView) findViewById(R.id.monCheminReinitialiserTextView)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.monCheminReinitialiserTextView)).setTextSize(20);
                ((TextView) findViewById(R.id.monCheminBoucleTextView)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.monCheminBoucleTextView)).setTextSize(20);
                
                ((TextView) findViewById(R.id.monCheminFenetreJuridiqueTextView)).setTypeface(policeAltermove);
                ((TextView) findViewById(R.id.monCheminFenetreJuridiqueTextView)).setTextSize(15);
                ((TextView) findViewById(R.id.monCheminFenetreJuridiqueTextView)).setText(texteCadreJuridique);
                

                
                
            }
   
   public void traitementEnergies()
   {
       //___________________ Traitement selon le type d'assistance
       filtrageEnergiesSelonAssistanceType();
       
        //__________________mise à jour info utilisateur

        if(infoUtilisateurtamponne == false)
        {

            utilisateur.setEnergieDistribueeSessionTampon(energieDistribuee);
            utilisateur.setEnergieProduiteSessionTampon(energieProduite);
            utilisateur.setEnergieTotaleSessionTampon(energieTotale);
            infoUtilisateurtamponne = true;
        }

        utilisateur.setEnergieProduiteSession(energieProduite - utilisateur.getEnergieProduiteSessionTampon());
        utilisateur.setEnergieDistribueeSession(energieDistribuee - utilisateur.getEnergieDistribueeSessionTampon());
        utilisateur.setEnergieTotaleSession(energieTotale- utilisateur.getEnergieTotaleSessionTampon());
       

        
       //___________________ Wh -> kcal
       double energieDepenseeKcal = (utilisateur.energieProduiteSession * 3.6 )/ 4.19;
       double participationALEffortTotal = 100 * utilisateur.energieProduiteSession/(utilisateur.energieDistribueeSession + utilisateur.energieProduiteSession);
       
       if(Double.isNaN(participationALEffortTotal))
       {
           participationALEffortTotal = 0;
       }
       
       //___________________affichage
       
       if(typeDeChemin.equals("cheminTDF"))
        {
            ((TextView)findViewById(R.id.cheminTDFParticipationALEffortTotalValueTextView)).setText(df.format( participationALEffortTotal));
            ((TextView)findViewById(R.id.cheminTDFEnergieDepenseeValueTextView)).setText(df.format(energieDepenseeKcal ));
        }
        if(typeDeChemin.equals("monChemin"))
        {
            ((TextView)findViewById(R.id.monCheminParticipationALEffortTotalValueTextView)).setText(df.format( participationALEffortTotal));
            ((TextView)findViewById(R.id.monCheminEnergieDepenseeValueTextView)).setText(df.format(energieDepenseeKcal ));
        }
        
        
        
   }
   
      public void filtrageEnergiesSelonAssistanceType()
      {
          //__________UP = Speed Sensor______DOWN = Torque Sensor
          
          if(assistanceType.equals("speedSensor"))
          {
              energieProduite = energieProduiteSpeedSensor;
              energieDistribuee = energieTotale - energieProduite;
              
          }
          else
          {
              energieProduite = energieProduiteTorqueSensor;
              energieDistribuee = energieTotale - energieProduite;              
          }
          
      }
   
   public void evolutionCredit()
   {
        if(credit == 0)
        { 
             versTableauRecapitulatif();
        }
        jaugeCreditD.weight = (credit);
        jaugeCreditG.weight = (creditInitial - credit);
        
        if(credit >= 0)
        {
            if(typeDeChemin.equals("monChemin"))
            {
                findViewById(R.id.monCheminCreditJaugeDroite).setLayoutParams(jaugeCreditD);
                findViewById(R.id.monCheminCreditJaugeGauche).setLayoutParams(jaugeCreditG);
                ((TextView) findViewById(R.id.monCheminDistanceRestanteTextView)).setText(Integer.toString(credit)+" m");
            }
        if(typeDeChemin.equals("cheminTDF"))
            {
                findViewById(R.id.cheminTDFCreditJaugeDroite).setLayoutParams(jaugeCreditD);
                findViewById(R.id.cheminTDFCreditJaugeGauche).setLayoutParams(jaugeCreditG);
                ((TextView) findViewById(R.id.cheminTDFDistanceRestanteTextView)).setText(Integer.toString(credit)+" m");
            }
        }
        
        
           
        
        

        
   }
   
   
     public void versTableauRecapitulatif()
           {
                    Intent intent2 = new Intent(CheminActivity.this, TableauFinal.class);

                    
                    intent2.putExtra("energieDistribueeTotale",energieDistribuee);
                    intent2.putExtra("energieProduiteTotale",energieProduite);
                    
                    intent2.putExtra("credit",credit);
                    intent2.putExtra("creditInitial",creditInitial);
                    
                    intent2.putExtra("distanceParcourueTotale",utilisateur.getDistanceTotale());
                    
                    intent2.putExtra("distanceParcourueDerniereSession",utilisateur.getDistanceSession());
                    intent2.putExtra("energieProduiteDerniereSession",utilisateur.getEnergieProduiteSession());
                    intent2.putExtra("energieDistribueeDerniereSession",utilisateur.getEnergieDistribueeSession());
                    
                    intent2.putExtra("assistanceType",assistanceType);
                    
                    startActivity(intent2);
                    finish();
           }
}